package com.neighbor.neighborhoodadmin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neighbor.neighborhoodadmin.pojo.Admins;
import com.neighbor.neighborhoodadmin.pojo.Posts;
import com.neighbor.neighborhoodadmin.service.*;
import com.neighbor.neighborhoodadmin.utils.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.HashMap;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 后台管理员表 前端控制器
 * </p>
 *
 * @author song
 * @since 2025-09-22 09:11:23
 */
@RestController
@RequestMapping("/admins")
public class AdminsController {
    @Autowired
    IAdminsService adminsService;
    @RequestMapping("login")       // 管理员登录
    ResponseMsg login(@RequestBody Admins admins){
        System.out.println("管理员登录数据: " + admins);
        QueryWrapper<Admins> wrapper = new QueryWrapper<>();
        wrapper.eq("username", admins.getUsername());
        Admins ret = adminsService.getOne(wrapper);
        // 判断账号存不存在
        if (ret == null){
            return ResponseMsg.getInstance(ResponseMsg.PARAMETER_ERROR, "账号不存在");
        }
        // 判断密码是否正确
        if (ret.getPassword().equals(admins.getPassword())){
            System.out.println("登录成功");
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "账号密码正确", ret);
        }
        return ResponseMsg.getInstance(ResponseMsg.PARAMETER_ERROR, "密码错误");
    }

     @Autowired
     IUsersService usersService;

    @Autowired
    IUserItemsService userItemsService;

    @Autowired
    IItemOrdersService itemOrdersService;

    @Autowired
    IPostsService postsService;

    @Autowired
    IAdminsService iAdminsService;  //管理员类代理对象

    /**
     *      添加数据库管理员对象
     * @param admins    新增管理员
     * @return  添加成功与否
     */
    @RequestMapping("add")
    ResponseMsg add(@RequestBody Admins admins){
        return iAdminsService.save(admins)?ResponseMsg.success("管理员添加成功"):
                ResponseMsg.getInstance(ResponseMsg.DATA_ERROR,"添加失败");
    }

    /**
     *      删除数据库管理员对象
     * @param adminId   删除管理员ID
     * @return  删除成功与否
     */
    @RequestMapping("del")
    ResponseMsg del(Integer adminId){
        if (null == adminId)
            return ResponseMsg.getInstance(ResponseMsg.PARAMETER_ERROR,"管理员删除失败");
        return iAdminsService.removeById(adminId)?ResponseMsg.success("管理员删除成功"):
                ResponseMsg.getInstance(ResponseMsg.DATA_ERROR,"管理员删除失败");
    }

    /**
     *      修改数据库管理员对象
     * @param admins    修改后的管理对象对象
     * @return  修改成功与否
     */
    @RequestMapping("revise")
    ResponseMsg revise(@RequestBody Admins admins){
        return iAdminsService.updateById(admins)?ResponseMsg.success("管理员修改成功"):
                ResponseMsg.getInstance(ResponseMsg.DATA_ERROR,"管理员修改失败");
    }

    /**
     *      搜索数据库管理员分页查询
     * @param page  页码数
     * @param limit 每页数量
     * @return 查询结果（IPage<Admins>）
     */
    @RequestMapping("page")
    ResponseMsg page(@RequestParam(defaultValue = "1") Integer page,
                     @RequestParam(defaultValue = "5") Integer limit){
        IPage<Admins> pages = new Page<>(page,limit);
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS,iAdminsService.page(pages));
    }

    /**
     *      根据管理员ID查询数据库管理员
     * @param adminId   管理员ID
     * @return  查询结果
     */
    @RequestMapping("selById")
    ResponseMsg selById(Integer adminId){
        System.out.println(adminId);
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS,iAdminsService.getById(adminId));
    }

    /**
     *      根据管理员昵称模糊查询
     * @param username  关键词
     * @return  查询结果（List<Admins>）
     */
    @RequestMapping("selByName")
    ResponseMsg selByName(String username){
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS,iAdminsService.selByName(username));
    }

    @RequestMapping("getAllCount")  //获取用户数量，物品数量，今日订单，待审核帖子,用hashmap返回
    ResponseMsg getUserCount(){

        HashMap<String, Long> countMap = new HashMap<>();
        //用户数量
        countMap.put("userCount", usersService.count());
        //物品数量
        countMap.put("itemCount", userItemsService.count());
        //今日订单数
        LocalDate today = LocalDate.now();
        countMap.put("orderCount", itemOrdersService.getTodayOrders(today));
        //待审核帖子数
        QueryWrapper<Posts> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 2); //2代表待审核
        countMap.put("postsCount", postsService.count(wrapper));
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS, countMap);
    }

    @RequestMapping("getHotPosts")  //获取热门贴列表
    ResponseMsg getHotPosts(){
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS,postsService.getHots());
    }

    @RequestMapping("delPostById/{postId}")  //根据ID删除帖子
    ResponseMsg delPostById(@PathVariable Integer postId){
        if (postId == null){
            return ResponseMsg.getInstance(ResponseMsg.DATA_ERROR, "传输错误id");
        }else {
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS,"删除成功", postsService.removeById(postId));
        }
    }


}
